package com.unicom.projt.util;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by IntelliJ IDEA.
 * User: Lee
 * Date: 11-12-16
 * Time: 下午10:38
 */
public class SimpleXmlParser {
    public static List<String> getElementsValue(String xml, String elementName) {
        List<String> list = new ArrayList<String>();
        Pattern pattern2 = Pattern.compile("<" + elementName + ">(.*?)</" + elementName + ">");
        Matcher matcher2 = pattern2.matcher(xml);
        while (matcher2.find()) {
            int i = 1;
            String data1 = matcher2.group(i);
            list.add(data1);
            i++;
        }
        return list;
    }

    public static String getResponseCode(String xml) {
        List<String> responseString = getElementsValue(xml, "Response");
        if (responseString.isEmpty()) return ResponseCode.UNKNOWN_ERROR;
        List<String> resCodeList = getElementsValue(responseString.get(0), "RspCode");
        if(resCodeList.isEmpty()) return ResponseCode.UNKNOWN_ERROR;
        return  resCodeList.get(0);
    }
    public static String getResponseDes(String xml) {
        List<String> responseString = getElementsValue(xml, "Response");
        if (responseString.isEmpty()) return ResponseCode.UNKNOWN_ERROR;
        List<String> resCodeList = getElementsValue(responseString.get(0), "RspDesc");
        if(resCodeList.isEmpty()) return ResponseCode.UNKNOWN_ERROR;
        return  resCodeList.get(0);
    }
    
    
    public static String getOrderId(String response){
        return response.substring(response.indexOf("OrderId")+8,response.indexOf("OrderId")+28);
    }
    public static String getPayPkg(String response){
        return response.substring(response.indexOf("PayPkg")+7,response.lastIndexOf("PayPkg")-2);
    }
}
